//
//  HXDPersonalCentreViewController.m
//  qingDaoCircleOfLife
//
//  Created by huxuedong on 15/11/27.
//  Copyright © 2015年 huxuedong. All rights reserved.
//

#import "HXDPersonalCentreViewController.h"
#import "AppDelegate.h"
#import "HXDHomeViewController.h"
#import "HXDLoginViewController.h"

@interface HXDPersonalCentreViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate>

@property (weak, nonatomic) IBOutlet UIButton *iconButton;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIButton *orderButton;
@property (weak, nonatomic) IBOutlet UIButton *changeInfoButton;
@property (weak, nonatomic) IBOutlet UIButton *changePwdButton;
@property (weak, nonatomic) IBOutlet UIButton *escButton;
@property (weak, nonatomic) IBOutlet UIButton *nilButton;

@end

@implementation HXDPersonalCentreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLabel.text = @"个人中心";
    self.rightButton.frame = CGRectMake(kUIScreenWidth - 2 * kNavigationButtonWidth + 30, kUIStatusBarHeight, kNavigationButtonWidth, kNavigationButtonWidth);
    [self.rightButton setTitle:nil forState:UIControlStateNormal];
    UIImage *icon = [UIImage imageWithData:[kSaveData objectForKey:kIconImage]];
    [self.rightButton setBackgroundImage:icon forState:UIControlStateNormal];
    self.rightButton.layer.cornerRadius = 20;
    self.rightButton.clipsToBounds = YES;
    self.orderButton.layer.cornerRadius = 5;
    self.orderButton.clipsToBounds = YES;
    self.iconButton.layer.cornerRadius = 65;
    self.iconButton.clipsToBounds = YES;
    UIImage *iconImage = [UIImage imageWithData:[kSaveData objectForKey:kIconImage]];
    [self.iconButton setBackgroundImage:iconImage forState:UIControlStateNormal];
    self.changeInfoButton.layer.cornerRadius = 5;
    self.changeInfoButton.clipsToBounds = YES;
    self.changePwdButton.layer.cornerRadius = 5;
    self.changePwdButton.clipsToBounds = YES;
    self.escButton.layer.cornerRadius = 5;
    self.escButton.clipsToBounds = YES;
    self.nilButton.layer.cornerRadius = 5;
    self.nilButton.clipsToBounds = YES;
}

- (IBAction)iconAction {
    //创建图片选择控制器
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    //图片选择器的数据源为相册
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    //设置图片选择控制器的代理为主控制器
    picker.delegate = self;
    //modal方法跳转控制器
    [self presentViewController:picker animated:YES completion:nil];
}

//图片选择器选择一张图片的时候就会调用
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    //设置选择图片的类型为原图
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    NSData *imageData = [[NSData alloc] init];
    if (UIImagePNGRepresentation(image) == nil) {
        imageData = UIImageJPEGRepresentation(image, 1);
    } else {
        imageData = UIImagePNGRepresentation(image);
    }
    [kSaveData setObject:imageData forKey:kIconImage];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.iconButton.bounds];
    imageView.image = image;
    [self.iconButton addSubview:imageView];
    //用modal方法回退控制器
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)exitAction {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"login" object:nil];
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"确定要退出吗？" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"退出" otherButtonTitles:nil, nil];
    [sheet showInView:self.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        [self.navigationController popViewControllerAnimated:YES];
        AppDelegate *app = [UIApplication sharedApplication].delegate;
        [app.tabBar isHiddenTabBar:NO];
    }
}

- (void)backMethod {
    HXDHomeViewController *homeViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"HXDHomeViewController"];
    [self.navigationController pushViewController:homeViewController animated:YES];
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    [app.tabBar isHiddenTabBar:YES];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
